package com.hundsun.cprs.yyt.service.form.service.impl;

import java.util.List;

import com.hundsun.cprs.yyt.service.form.domain.FormAttrValues;
import com.hundsun.cprs.yyt.service.form.service.FormAttrValuesService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.hundsun.cprs.yyt.common.base.BaseService;
import com.hundsun.cprs.yyt.service.form.dao.FormAttrValuesDao;
import com.hundsun.cprs.yyt.service.form.domain.FormAttrValues;
import com.hundsun.cprs.yyt.service.form.service.FormAttrValuesService;

@Service
public class FormAttrValuesServiceImpl extends BaseService implements FormAttrValuesService {
    
    @Autowired
    private FormAttrValuesDao formAttrValuesDao;
    
    @Override
    public List<FormAttrValues> selectByFormIdAndObjectId(Long formId, Long objectId) {
        return formAttrValuesDao.selectByFormIdAndObjectId(formId, objectId);
    }

    @Override
    @Transactional
    public void createFormAttrValues(List<FormAttrValues> attrValuesList) {
        for(FormAttrValues item : attrValuesList){
            if (StringUtils.isNotEmpty(item.getAttrValue())) {
                item.setIsDeleted("N");
                formAttrValuesDao.insert(item);
            }
        }
    }

    @Override
    @Transactional
    public void updateFormAttrValues(List<FormAttrValues> attrValuesList) {
        if (attrValuesList == null || attrValuesList.size() == 0) {
            return;
        }
        Long formId = attrValuesList.get(0).getFormId();
        Long objectId = attrValuesList.get(0).getObjectId();
        //删除原属性
        formAttrValuesDao.deleteByFormIdAndObjectId(formId, objectId);
        //新增属性
        createFormAttrValues(attrValuesList);
    }

    @Override
    @Transactional
    public void deleteFormAttrValues(Long formId, Long objectId) {
        formAttrValuesDao.deleteByFormIdAndObjectId(formId, objectId);
    }

}
